package com.yyz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyz.common.BaseContext;
import com.yyz.domain.ShoppingCart;
import com.yyz.mapper.ShoppingCartMapper;
import com.yyz.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yyz
 * @create 2023-07-26 21:14
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        lqw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(lqw);
        return shoppingCarts;
    }

    /**
     * 添加菜品或套餐到购物车
     *
     * @param shoppingCart
     */
    @Override
    public void add(ShoppingCart shoppingCart) {
        //设置user_id
        Long id = BaseContext.getThreadLocal();
        shoppingCart.setUserId(id);

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, id);
        //判断是套餐还是菜品
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //是菜品
            lqw.eq(ShoppingCart::getDishId, dishId)
                    .eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
        } else {
            //是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //判断是否已经添加
        ShoppingCart sc = shoppingCartMapper.selectOne(lqw);
        if (sc != null) {
            //已添加 num+1
            sc.setNumber(sc.getNumber() + 1);
            shoppingCartMapper.updateById(sc);
            return;
        }
        //未添加 添加进数据库
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getThreadLocal());

        shoppingCartMapper.delete(lqw);
    }

    /**
     * 数量-1
     *
     * @param shoppingCart
     */
    @Override
    public void sub(ShoppingCart shoppingCart) {
        //判断是套餐还是菜品
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        if (dishId != null) {
            //是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart sc = shoppingCartMapper.selectOne(lqw);
        Integer number = sc.getNumber();
        //判断数量是否为1
        if (number == 1) {
            //为1 直接删除
            shoppingCartMapper.deleteById(sc);
        } else {
            //不为1 num-1
            sc.setNumber(number - 1);
            shoppingCartMapper.updateById(sc);
        }
    }
}
